var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    popupShow: false,
    statusSwitch: false,
    typePage: 1, //页面类型1:编辑2:添加
    name: '',
    money: '',
    describe: '',
    status: 1,
    id: '',
    form_id: 1, //验证表单重复提交
    villageList: [], //园区列表
    selectVillageList: [], //已选择的园区
    search: "", //搜索内容
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    var typePage = options.type;
    that.data.id = options.id;
    if (typePage == 1) {
      wx.setNavigationBarTitle({
        title: '编辑'
      });
      that.getPromotion();
    } else if (typePage == 2) {
      wx.setNavigationBarTitle({
        title: '添加'
      })
      this.getVillageList();
    }
    that.setData({
      typePage: typePage,
    });

  },





  //显示POP
  showPop() {
    this.setData({
      popupShow: true
    })
  },


  //隐藏POP
  hidePopup() {
    this.setData({
      popupShow: false
    })
  },

  //POP确定
  submitCheck() {
    const list = this.data.villageList;
    const result = list.filter(item => {
      return !!item.checked;
    })
    this.setData({
      selectVillageList: result,
      popupShow: false
    })
  },



  //点击园区徽章
  chooseVillageItem(e) {
    const {
      index
    } = e.currentTarget.dataset;
    let villageList = this.data.villageList;
    villageList[index].checked = !villageList[index].checked;
    this.setData({
      villageList
    })
  },

  //园区搜索
  searchInput(e) {
    console.log(e.detail.value);
    const village = e.detail.value.trim();
    const list = this.data.villageList;
    if (village === "") { //清空搜索内容会map所有内容显示，然后将勾选的排序到最上面
      list.map((item, index) => {
        return item.hidden = false;
      })
      list.sort((a, b) => {
        return Boolean(b.checked) - Boolean(a.checked);
      })
    } else {
      list.forEach((item, index) => {
        let conditionVillage = !item.village_name.includes(village);
        let conditionProvince = !item.province_name.includes(village);
        let conditionCity = !item.city_name.includes(village);
        let conditionArea = !item.area_name.includes(village);
        if (conditionVillage && conditionProvince && conditionCity && conditionArea) {
          list[index].hidden = true;
        }
      })
    }
    this.setData({
      search: village,
      villageList: list
    })
  },


  searchInputConfrim(e) {

  },


  //获取园区列表
  getVillageList(data = "") {
    app.ajax({
      url: "Partner/WaterCard/getPartnerVillage",
      success: res => {
        if (res.data.code == 1000) {
          const villageList = res.data.data;
          let selectVillageList = [];
          if (data !== "" && data instanceof Object && data.village_id) {
            let arr = data.village_id;
            villageList.forEach((item, index) => {
              if (arr.includes(item.id)) {
                villageList[index].checked = true;
                selectVillageList.push(item);
              }
            })
            villageList.sort((a, b) => {
              return Boolean(b.checked) - Boolean(a.checked)
            });
          }
          this.setData({
            villageList,
            selectVillageList
          })
          console.log("园区列表", villageList);
        }
      }
    })
  },


  baseCheck(type = 1) {
    const search = this.data.search;
    let list = this.data.villageList;
    if (search !== "") {
      list.map((item, index) => {
        if (!item.hidden) {
          if (type == 1) {
            return item.checked = true;
          } else if (type == -1) {
            return item.checked = false;
          } else if (type == 0) {
            return item.checked = !item.checked
          }
        }

      })
    } else {
      list.map((item, index) => {
        if (type == 1) {
          return item.checked = true;
        } else if (type == -1) {
          return item.checked = false;
        } else if (type == 0) {
          return item.checked = !item.checked
        }
      })
    }
    this.setData({
      villageList: list
    })
  },

  //全选操作
  checkedAll() {
    this.baseCheck();
  },
  //置空=全不选操作
  restCheck() {
    this.baseCheck(-1);
  },

  //反选操作
  reverseCheck() {
    this.baseCheck(0);
  },



  /**
   * 获取详情
   */
  getPromotion() {
    var that = this;
    app.ajax({
      url: 'Partner/Promotion/getPromotion',
      data: {
        id: that.data.id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          var status = true;
          if (res.data.data.status == 2) {
            status = false;
          }
          that.setData({
            name: res.data.data.name,
            money: res.data.data.money,
            describe: res.data.data.describe,
            status: status
          })
          that.getVillageList(res.data.data);
        } else {
          app.showToast(res.data.message)
        }
      }
    })
  },
  add() {
    var that = this;
    var promotion_name = that.data.name; // 项目名称
    var promotion_money = that.data.money; // 金额
    var promotion_describe = that.data.describe; // 描述
    var id = that.data.id; // id
    var status = 1; // 状态
    var form_id = that.data.form_id;
    console.log(form_id, "form_id");
    this.setData({
      form_id: 0
    });
    if (that.data.status === false) {
      status = 2;
    }
    if (promotion_name == '') {
      app.showToast('项目名不能为空');
      return false;
    }
    if (promotion_money == '') {
      app.showToast('金额不能为空');
      return false;
    }
    if (this.data.villageList.length == 0) {
      app.showToast('请先添加园区');
      return;
    }
    if (this.data.selectVillageList.length == 0) {
      app.showToast("请至少选择一个园区");
      return;
    }
    let village_id = [];
    this.data.selectVillageList.forEach(item => {
      village_id.push(item.id)
    })
    app.ajax({
      url: 'Partner/Promotion/promotionAdd',
      data: {
        promotion_name: promotion_name,
        promotion_money: promotion_money,
        id: id,
        promotion_describe: promotion_describe,
        status: status,
        form_id: form_id,
        village_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          app.showToast(res.data.message);
          setTimeout(function () {
            let currentPages = getCurrentPages();
            if (currentPages.length >= 2) {
              const targetPage = currentPages[currentPages.length - 2];
              if (targetPage.route.includes("projectLists/projectLists")) {
                targetPage.reloadData();
                wx.navigateBack({
                  delta: 1,
                })
              } else {
                wx.redirectTo({
                  url: '../projectLists/projectLists',
                })
              }
            }
          }, 1500);

        } else {
          app.showToast(res.data.message);
          that.setData({
            form_id: 1
          })
        }
      },
      error: () => {
        that.setData({
          form_id: 1
        })
      }
    })
  },
  switchChange(e) {
    this.setData({
      status: e.detail.value
    })
  },
  //名称
  editName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  //金额
  editMoney(e) {
    this.setData({
      money: e.detail.value
    })
  },
  //描述
  editDescribe(e) {
    this.setData({
      describe: e.detail.value
    })
  },
})